package com.pwl.springBoot.modules.test.controller;

import com.pwl.springBoot.modules.common.vo.Result;
import com.pwl.springBoot.modules.test.entity.Student;
import com.pwl.springBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student  ----post
     * {"studentName":"XieQiuyu","age":26}
     * {"studentName":"NiHuijie","age":24, "card":{"cardNo":"0213"}}
     * {"id":1,"studentName":"XieQiuyu","age":27}
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public Result<Student> editStudent(@RequestBody Student student){
        return studentService.editStudent(student);
    }

    /**
     * 127.0.0.1/api/student/2  ----get
     */
    @GetMapping(value = "/student/{id}")
    public Student getStudentById(@PathVariable int id){
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1/api/student?studentName=pwl ---- get
     */
    @GetMapping(value="/student")
    public Student getStudentByStudentName(@RequestParam String studentName) {
        return studentService.getStudentByStudentName(studentName);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"studentName":"pwl","id":1}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/students ---- post
     * {"currentPage":1,"pageSize":5,"sort":"id","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearch(@RequestBody Search search) {
        return studentService.getStudentsBySearch(search);
    }
}
